package bigdata

import (
	"context"
	"errors"
	"git.mycaigou.com/gfyx/common/http_client"
	"git.mycaigou.com/gfyx/common/simplecopier"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/utils"
	"git.myscrm.cn/golang/stark/v4"
)

/**
 * @Author: xiuliu
 * @Description:
 * @File:  bigdata_repository
 * @Version: 1.0.0
 * @Date: 2023/12/14 10:47
 */

const (
	getCustomerMonitorUri  = "/big-data/gfyx/customer/monitor-list"
	getBuildHistogramUri   = "/big-data/business-opportunity/project/build-histogram"
	getSaleHistogramUri    = "/big-data/business-opportunity/project/sale-histogram"
	getLandHistogramUri    = "/big-data/business-opportunity/project/land-histogram"
	getLandTopListUri      = "/big-data/business-opportunity/project/land-top-list"
	getSaleTopListUri      = "/big-data/business-opportunity/project/sale-top-list"
	getBuildTopListUri     = "/big-data/business-opportunity/project/build-top-list"
	getCustomerPirChartUri = "/big-data/business-opportunity/customer/pie-chart"
	getCustomerTopListUri  = "/big-data/business-opportunity/customer/top-list"
	getLineChartUri        = "/big-data/business-opportunity/market-insight/line-chart"
	getBidHistogramUri     = "/big-data/business-opportunity/market-insight/bid-histogram"
	getBidTopListUri       = "/big-data/business-opportunity/market-insight/bid-top-list"
	GetCompanyTargetUri    = "/big-data/gfyx/company/target"
)

type bigDataRepository struct{}

func NewBigdataRepository() BigdataRepositoryIface {
	return &bigDataRepository{}
}

func (r *bigDataRepository) GetCustomerMonitorList(ctx context.Context, request *GetCustomerMonitorRequest) (*CustomerMonitor, error) {
	if request == nil || request.TenantCode == "" || request.TenantUserId == "" {
		return nil, errors.New("租户code,用户id不能为空")
	}
	if request.Page <= 0 {
		request.Page = 1
	}
	if request.PageSize <= 0 {
		request.PageSize = 20
	}
	client := http_client.NewDataServiceClient()
	params := utils.MyStruct2Map(request)
	response := GetCustomerMonitorResponse{}
	_, _, errs := client.Post(ctx, getCustomerMonitorUri, params, nil).EndStruct(&response)
	if len(errs) > 0 {
		stark.Logger.Errorf(ctx, "get bigdata_service GetCustomerMonitorList error:%+v", errs)
		return nil, errs[0]
	}

	if response.Success == false {
		return nil, errors.New(response.Message)
	}
	return response.Data, nil
}

func (r *bigDataRepository) GetBuildHistogram(ctx context.Context, request *MarketInsightCommonRequest) (*BuildHistogramData, error) {
	if request == nil || request.TimeBegin == "" || request.TimeEnd == "" {
		return nil, errors.New("GetBuildHistogram 筛选时间不能为空")
	}
	if request.ProvinceCode == "" && request.CityCode == "" {
		return nil, errors.New("GetBuildHistogram 省市区不能为空")
	}

	params := map[string]interface{}{
		"city_code":     request.CityCode,
		"time_begin":    request.TimeBegin,
		"time_end":      request.TimeEnd,
		"province_code": request.ProvinceCode,
	}
	client := http_client.NewDataServiceClient()
	response := ProjectBuildHistogramResponse{}
	_, _, errs := client.Post(ctx, getBuildHistogramUri, params, nil).EndStruct(&response)
	if len(errs) > 0 {
		stark.Logger.Errorf(ctx, "bigdata_service GetBuildHistogram error:%+v", errs)
		return nil, errs[0]
	}
	if response.Success == false {
		return nil, errors.New(response.Message)
	}
	return response.Data, nil
}

func (r *bigDataRepository) GetSaleHistogram(ctx context.Context, request *MarketInsightCommonRequest) (*SaleHistogramData, error) {
	if request == nil || request.TimeBegin == "" || request.TimeEnd == "" {
		return nil, errors.New("GetSaleHistogram 筛选时间不能为空")
	}
	if request.ProvinceCode == "" && request.CityCode == "" {
		return nil, errors.New("GetSaleHistogram 省市区不能为空")
	}

	params := map[string]interface{}{
		"city_code":     request.CityCode,
		"time_begin":    request.TimeBegin,
		"time_end":      request.TimeEnd,
		"province_code": request.ProvinceCode,
	}
	client := http_client.NewDataServiceClient()
	response := ProjectSaleHistogramResponse{}
	_, _, errs := client.Post(ctx, getSaleHistogramUri, params, nil).EndStruct(&response)
	if len(errs) > 0 {
		stark.Logger.Errorf(ctx, "bigdata_service GetSaleHistogram error:%+v", errs)
		return nil, errs[0]
	}
	if response.Success == false {
		return nil, errors.New(response.Message)
	}
	return response.Data, nil
}

func (r *bigDataRepository) GetLandHistogram(ctx context.Context, request *MarketInsightCommonRequest) (*LandHistogramData, error) {
	if request == nil || request.TimeBegin == "" || request.TimeEnd == "" {
		return nil, errors.New("GetLandHistogram 筛选时间不能为空")
	}
	if request.ProvinceCode == "" && request.CityCode == "" {
		return nil, errors.New("GetLandHistogram 省市区不能为空")
	}

	params := map[string]interface{}{
		"city_code":     request.CityCode,
		"time_begin":    request.TimeBegin,
		"time_end":      request.TimeEnd,
		"province_code": request.ProvinceCode,
	}
	client := http_client.NewDataServiceClient()
	response := ProjectLandHistogramResponse{}
	_, _, errs := client.Post(ctx, getLandHistogramUri, params, nil).EndStruct(&response)
	if len(errs) > 0 {
		stark.Logger.Errorf(ctx, "bigdata_service GetLandHistogram error:%+v", errs)
		return nil, errs[0]
	}
	if response.Success == false {
		return nil, errors.New(response.Message)
	}
	return response.Data, nil
}

func (r *bigDataRepository) GetLandTopCompany(ctx context.Context, request *MarketInsightTopListRequest) (*ProjectTopListData, error) {
	if request == nil || request.TimeBegin == "" || request.TimeEnd == "" {
		return nil, errors.New("GetLandTopCompany 筛选时间不能为空")
	}
	if request.ProvinceCode == "" {
		return nil, errors.New("GetLandTopCompany 省份地区不能为空")
	}
	params := map[string]interface{}{
		"city_code":     request.CityCode,
		"time_begin":    request.TimeBegin,
		"time_end":      request.TimeEnd,
		"province_code": request.ProvinceCode,
		"page":          request.Page,
		"page_size":     request.PageSize,
	}
	client := http_client.NewDataServiceClient()
	response := GetProjectTopListResponse{}
	_, _, errs := client.Post(ctx, getLandTopListUri, params, nil).EndStruct(&response)
	if len(errs) > 0 {
		stark.Logger.Errorf(ctx, "bigdata_service GetLandTopCompany error:%+v", errs)
		return nil, errs[0]
	}
	if response.Success == false {
		return nil, errors.New(response.Message)
	}
	return response.Data, nil
}

func (r *bigDataRepository) GetSaleTopCompany(ctx context.Context, request *MarketInsightTopListRequest) (*ProjectTopListData, error) {
	if request == nil || request.TimeBegin == "" || request.TimeEnd == "" {
		return nil, errors.New("GetSaleTopCompany 筛选时间不能为空")
	}
	if request.ProvinceCode == "" {
		return nil, errors.New("GetSaleTopCompany 省份地区不能为空")
	}
	params := map[string]interface{}{
		"city_code":     request.CityCode,
		"time_begin":    request.TimeBegin,
		"time_end":      request.TimeEnd,
		"province_code": request.ProvinceCode,
		"page":          request.Page,
		"page_size":     request.PageSize,
	}
	client := http_client.NewDataServiceClient()
	response := GetProjectTopListResponse{}
	_, _, errs := client.Post(ctx, getSaleTopListUri, params, nil).EndStruct(&response)
	if len(errs) > 0 {
		stark.Logger.Errorf(ctx, "bigdata_service GetSaleTopCompany error:%+v", errs)
		return nil, errs[0]
	}
	if response.Success == false {
		return nil, errors.New(response.Message)
	}
	return response.Data, nil
}

func (r *bigDataRepository) GetBuildTopCompany(ctx context.Context, request *MarketInsightTopListRequest) (*ProjectTopListData, error) {
	if request == nil || request.TimeBegin == "" || request.TimeEnd == "" {
		return nil, errors.New("GetBuildTopCompany 筛选时间不能为空")
	}
	if request.ProvinceCode == "" {
		return nil, errors.New("GetBuildTopCompany 省份地区不能为空")
	}
	params := map[string]interface{}{
		"city_code":     request.CityCode,
		"time_begin":    request.TimeBegin,
		"time_end":      request.TimeEnd,
		"province_code": request.ProvinceCode,
		"page":          request.Page,
		"page_size":     request.PageSize,
	}
	client := http_client.NewDataServiceClient()
	response := GetProjectTopListResponse{}
	_, _, errs := client.Post(ctx, getBuildTopListUri, params, nil).EndStruct(&response)
	if len(errs) > 0 {
		stark.Logger.Errorf(ctx, "bigdata_service GetBuildTopCompany error:%+v", errs)
		return nil, errs[0]
	}
	if response.Success == false {
		return nil, errors.New(response.Message)
	}
	return response.Data, nil
}

func (r *bigDataRepository) GetCompanyTarget(ctx context.Context, request *GetCompanyTargetRequest) (*GetCompanyTargetData, error) {
	client := http_client.NewDataServiceClient()
	response := GetCompanyTargetResponse{}
	requestMap, _ := simplecopier.StructToMapByJsonTag(request)
	_, _, errs := client.Post(ctx, GetCompanyTargetUri, requestMap, nil).EndStruct(&response)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	if response.Success == false {
		return nil, errors.New(response.Message)
	}
	return response.Data, nil
}

func (r *bigDataRepository) GetCustomerPieChart(ctx context.Context, request *MarketInsightCommonRequest) (*CustomerPieChartData, error) {
	if request == nil || request.TimeBegin == "" || request.TimeEnd == "" {
		return nil, errors.New("GetCustomerPieChart 筛选时间不能为空")
	}
	if request.ProvinceCode == "" && request.CityCode == "" {
		return nil, errors.New("GetCustomerPieChart 省市区不能为空")
	}

	params := map[string]interface{}{
		"city_code":     request.CityCode,
		"time_begin":    request.TimeBegin,
		"time_end":      request.TimeEnd,
		"province_code": request.ProvinceCode,
	}
	client := http_client.NewDataServiceClient()
	response := GetCustomerPieChartResponse{}
	_, _, errs := client.Post(ctx, getCustomerPirChartUri, params, nil).EndStruct(&response)
	if len(errs) > 0 {
		stark.Logger.Errorf(ctx, "bigdata_service GetCustomerPieChart error:%+v", errs)
		return nil, errs[0]
	}
	if response.Success == false {
		return nil, errors.New(response.Message)
	}
	return response.Data, nil
}

func (r *bigDataRepository) GetCustomerTopList(ctx context.Context, request *MarketInsightTopListRequest) (*CustomerTopListData, error) {
	if request == nil || request.TimeBegin == "" || request.TimeEnd == "" {
		return nil, errors.New("GetCustomerPieChart 筛选时间不能为空")
	}
	if request.ProvinceCode == "" && request.CityCode == "" {
		return nil, errors.New("GetCustomerPieChart 省市区不能为空")
	}

	params := map[string]interface{}{
		"city_code":     request.CityCode,
		"time_begin":    request.TimeBegin,
		"time_end":      request.TimeEnd,
		"province_code": request.ProvinceCode,
		"page":          request.Page,
		"page_size":     request.PageSize,
	}
	client := http_client.NewDataServiceClient()
	response := GetCustomerTopListResponse{}
	_, _, errs := client.Post(ctx, getCustomerTopListUri, params, nil).EndStruct(&response)
	if len(errs) > 0 {
		stark.Logger.Errorf(ctx, "bigdata_service GetCustomerPieChart error:%+v", errs)
		return nil, errs[0]
	}
	if response.Success == false {
		return nil, errors.New(response.Message)
	}
	return response.Data, nil
}

func (r *bigDataRepository) GetLineChart(ctx context.Context, request *MarketInsightCommonRequest) (*LineChartData, error) {
	if request == nil || request.TimeBegin == "" || request.TimeEnd == "" {
		return nil, errors.New("GetCustomerPieChart 筛选时间不能为空")
	}
	if request.ProvinceCode == "" && request.CityCode == "" {
		return nil, errors.New("GetCustomerPieChart 省市区不能为空")
	}

	params := map[string]interface{}{
		"city_code":     request.CityCode,
		"time_begin":    request.TimeBegin,
		"time_end":      request.TimeEnd,
		"province_code": request.ProvinceCode,
		"page":          1,
		"page_size":     20,
	}
	client := http_client.NewDataServiceClient()
	response := GetLineChartResponse{}
	_, _, errs := client.Post(ctx, getLineChartUri, params, nil).EndStruct(&response)
	if len(errs) > 0 {
		stark.Logger.Errorf(ctx, "bigdata_service GetCustomerPieChart error:%+v", errs)
		return nil, errs[0]
	}
	if response.Success == false {
		return nil, errors.New(response.Message)
	}
	return response.Data, nil
}

func (r *bigDataRepository) GetBidHistogram(ctx context.Context, request *MarketInsightCommonRequest) (*GetBidHistogramData, error) {
	if request == nil || request.TimeBegin == "" || request.TimeEnd == "" {
		return nil, errors.New("GetBidHistogram 筛选时间不能为空")
	}
	if request.ProvinceCode == "" && request.CityCode == "" {
		return nil, errors.New("GetBidHistogram 省市区不能为空")
	}

	params := map[string]interface{}{
		"city_code":     request.CityCode,
		"time_begin":    request.TimeBegin,
		"time_end":      request.TimeEnd,
		"province_code": request.ProvinceCode,
	}
	client := http_client.NewDataServiceClient()
	response := GetBidHistogramResponse{}
	_, _, errs := client.Post(ctx, getBidHistogramUri, params, nil).EndStruct(&response)
	if len(errs) > 0 {
		stark.Logger.Errorf(ctx, "bigdata_service GetBidHistogram error:%+v", errs)
		return nil, errs[0]
	}
	if response.Success == false {
		return nil, errors.New(response.Message)
	}
	return response.Data, nil
}

func (r *bigDataRepository) GetBidTopCompany(ctx context.Context, request *MarketInsightTopListRequest) (*GetBidTop20CompanyData, error) {
	if request == nil || request.TimeBegin == "" || request.TimeEnd == "" {
		return nil, errors.New("GetBidTopCompany 筛选时间不能为空")
	}
	if request.ProvinceCode == "" && request.CityCode == "" {
		return nil, errors.New("GetBidTopCompany 省市区不能为空")
	}

	params := map[string]interface{}{
		"city_code":     request.CityCode,
		"time_begin":    request.TimeBegin,
		"time_end":      request.TimeEnd,
		"province_code": request.ProvinceCode,
		"page":          request.Page,
		"page_size":     request.PageSize,
	}
	client := http_client.NewDataServiceClient()
	response := GetBidTop20CompanyResponse{}
	_, _, errs := client.Post(ctx, getBidTopListUri, params, nil).EndStruct(&response)
	if len(errs) > 0 {
		stark.Logger.Errorf(ctx, "bigdata_service GetBidTopCompany error:%+v", errs)
		return nil, errs[0]
	}
	if response.Success == false {
		return nil, errors.New(response.Message)
	}
	return response.Data, nil
}
